package game.screens;

import game.Game;
import game.Main;
import game.util.ChatModule;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 * The screen you see when the game is waiting for players.
 */
public class WaitingScreen extends BasicGameState {

   public static final int ID = 1;
   private ChatModule chatModule;
   private TextField textField;
   /**
    * Keyboard input
    */
   private Input input;
   private Game game;
   
   public WaitingScreen(Game game) {
      this.game = game;
   }
   
   /**
    * Starts up the screen
    */
   @Override
   public void init(GameContainer container, StateBasedGame arg1)
         throws SlickException {
      input = container.getInput();
      textField = new TextField(container, container.getDefaultFont(), 0, Main.HEIGHT - 20, Main.WIDTH, 20);
      chatModule = new ChatModule(game);
   }

   /**
    * Rendering. Called based on framerate.
    */
   @Override
   public void render(GameContainer container, StateBasedGame arg1, Graphics g)
         throws SlickException {
      textField.render(container, g);
      chatModule.render(container, g, 5, 35);
   }

   @Override
   public void update(GameContainer container, StateBasedGame arg1, int arg2)
         throws SlickException {
      if (input.isKeyPressed(Input.KEY_ENTER) && textField.getText().length() > 0) {
         game.getSession().writeObject(textField.getText());
         textField.setText("");
      }
      chatModule.update();
   }

   @Override
   public int getID() {
      return ID;
   }

}
